angular.module('magazineservice', [])
  .factory('Magazine', function($resource,$rootScope,ENV) {

    var APIUrl = ENV.api;
    var magazineListJson,pageSize=16,bookListJson,hasBookDetailJson,saveMagazineId,saveBdIdListJson,saveBdFromWhere,
      chapterListJson,dbcontentJson,collectBdJson,likeBdJson,collectMagazineJson,fromWhereJson;
    var magazineListResource = $resource(APIUrl+"bookshelf/getMagazineList.do");
    var bookListResource = $resource(APIUrl+"bookshelf/getBookList.do");
    var magazineChapterResource = $resource(APIUrl+"magazine/getChapterListById.do");
    var bdResource = $resource(APIUrl+"magazine/getChapterDetailById.do");
    var likeBdResource = $resource(APIUrl+"magazine/likeChapterDetail.do");
    var collectBdResource = $resource(APIUrl+"magazine/collectMagazineDetail.do");
    var collectMagazineResource = $resource(APIUrl+"magazine/collectMagazine.do");
    var hasBookDetailResource = $resource(APIUrl+"magazine/hasbookDetail.do");

    return{
      saveBdFromWhere:function(fromWhere){
        saveBdFromWhere = fromWhere;
      },
      getBdFromWhere:function(){
        return saveBdFromWhere;
      },
      //保存是从何处进入书的章节目录的
      saveBdIdList:function(bdIdList){
        saveBdIdListJson = bdIdList;
      },
      getBdIdList:function(){
        return saveBdIdListJson;
      },
      saveFromWhere:function(fromWhere){
        fromWhereJson = fromWhere;
      },
      getFromWhere:function(){
        return fromWhereJson;
      },
      saveMagazineId:function(magazineId){
        saveMagazineId = magazineId;
      },
      getMagazineId:function(){
        return saveMagazineId;
      },
      //根据章节的编号获取到章节，获取到是否存在章节
      getBookDetail:function(bdId){
        hasBookDetailResource.save({
          bd_id:bdId
        },function(response){
          hasBookDetailJson = response.hasbookDetail;
          $rootScope.$broadcast('Book.hasBookDetailUpdated');
        });
      },
      hasBookDetail:function(){
        return hasBookDetailJson;
      },
      //获取丛书列表
      getBookList:function(memberId){
        var hasNextPage = true;
        bookListResource.save({
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.bookList.length<pageSize){
            hasNextPage = false;
          }
          bookListJson={
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.bookList
          };
          $rootScope.$broadcast('Book.bookListUpdated');
        });
      },
      //加载更多书籍
      getMoreBookList:function(memberId){
        if(bookListJson===undefined){
          return false;
        }
        var hasNextPage=bookListJson.hasNextPage;
        var nextPage=bookListJson.nextPage;
        var moreCommentData=bookListJson.data;
        if(!hasNextPage){
          return false;
        }
        bookListResource.save({
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.bookList.length<pageSize){
            hasNextPage = false;
          }
          moreCommentData = moreCommentData.concat(response.bookList);
          bookListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreCommentData
          };
          $rootScope.$broadcast('Book.MoreBookListUpdated');
        });
      },
      //获取到杂志列表
      getMagazineList:function(memberId){
        var hasNextPage = true;
        magazineListResource.save({
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.magazineList.length<pageSize){
            hasNextPage = false;
          }
          magazineListJson={
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.magazineList
          };
          $rootScope.$broadcast('Magazine.magazineListUpdated');
        });
      },
      //加载更多杂志
      getMoreMagazineList:function(memberId){
        if(magazineListJson===undefined){
          return false;
        }
        var hasNextPage=magazineListJson.hasNextPage;
        var nextPage=magazineListJson.nextPage;
        var moreCommentData=magazineListJson.data;
        if(!hasNextPage){
          return false;
        }
        magazineListResource.save({
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.magazineList.length<pageSize){
            hasNextPage = false;
          }
          moreCommentData = moreCommentData.concat(response.magazineList);
          magazineListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreCommentData
          };
          $rootScope.$broadcast('Magazine.MoreMagazineListUpdated');
        });
      },
      //是否有下一页
      magazineHasNextPage:function(){
        if(magazineListJson===undefined){
          return false;
        }
        return magazineListJson.hasNextPage;
      },
      bookHasNextPage:function(){
          if(bookListJson===undefined){
            return false;
          }
        return bookListJson.hasNextPage;
      },
      //获取章节的信息信息
      getChapterListInfo:function(magazineId){
        magazineChapterResource.save({
          book_id:magazineId
        },function(response){
          chapterListJson = response;
          $rootScope.$broadcast('Magazine.chapterListUpdated');
        });
      },
      //获取章节的信息内容
      getContent:function(bdId,memberId){
        bdResource.save({
          book_detail_id:bdId,
          member_id:memberId
        },function(response){
          dbcontentJson = response;
          $rootScope.$broadcast('Magazine.BookDetailUpdated');
        });
      },
      //收藏该书籍
      collectBook:function(memberId,bookId,bookType){
        collectMagazineResource.save({
          member_id:memberId,
          mc_obj_id:bookId,
          book_type:bookType
        },function(response){
          collectMagazineJson = response;
          $rootScope.$broadcast('Magazine.CollectMagazineUpdated');
        });
      },
      //返回杂志列表的json数据
      getMagazineListJson:function(){
        if(magazineListJson===undefined){
          return false;
        }
       return  magazineListJson.data;
      },
      getBookListJson:function(){
        if(bookListJson===undefined){
          return false;
        }
        return bookListJson.data;
      },
      //返回章节列表Json数据
      getChapterListJson:function(){
        return chapterListJson;
      },
      //获取到章节的信息的Json
      getContentJson:function(){
        return dbcontentJson;
      },
      //收藏该章节详情
      collectBd:function(bdId,memberId){
        collectBdResource.save({
          member_id:memberId,
          mc_obj_id:bdId
        },function(response){
          collectBdJson = response;
          $rootScope.$broadcast('Magazine.collectBdUpdated');
        });
      },
      //点赞该章节详情
      likeBd:function(bdId,memberId){
        likeBdResource.save({
          member_id:memberId,
          book_detail_id:bdId
        },function(response){
          likeBdJson = response;
          $rootScope.$broadcast('Magazine.likeBdUpdated');
        });
      },
      //返回收藏的信息
      getCollectJson:function(){
        return collectBdJson;
      },
      //返回点赞的信息
      getLikeJson:function(){
        return likeBdJson;
      },
      //返回收藏书籍的Json数据
      getCollectMagazineJson:function(){
        return collectMagazineJson;
    }
    }
  })



//评论的Service
  .factory('MagazineComment', function($resource,$rootScope,ENV) {
    var APIUrl = ENV.api;

    var commentListJson,pageSize=15,memberInfo,bdTitle,
      magazineCommentJson,commentCount;

    var magazineCommentResource = $resource(APIUrl+"magazine/getCdCommentList.do");
    var publishCommentResource = $resource(APIUrl+"magazine/commentMagazine.do");
    var commentCountResource = $resource(APIUrl+"magazine/getCommentCount.do");
    var deleteCommentResource = $resource(APIUrl+"magazine/delMagazineComment.do");

    //获取的新闻的评论列表
    return {
      //删除评论
      deleteComment:function(commentId,memberId,bdId){
        deleteCommentResource.save({
          bd_comment_id:commentId,
          member_id:memberId,
          book_detail_id:bdId
        },function(response){
          deleteCommentJson = response;
          $rootScope.$broadcast('MagazineComment.deleteCommentUpdated');
        });
      },
      getDelCommentJson:function(){
        return deleteCommentJson;
      },
      getCommentList:function(bdId,memberId){
        var hasNextPage = true;
        magazineCommentResource.save({
          book_detail_id:bdId,
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.commentList.length<pageSize){
            hasNextPage = false;
          }
          memberInfo = response.memberInfo;
          bdTitle = response.bdTitle;
          commentListJson = {
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.commentList
          };
          $rootScope.$broadcast('Magazine.CommentUpdated');
        })
      },
      getMoreCommentList:function(bdId,memberId){
        if(commentListJson===undefined){
          return false;
        }
        var hasNextPage=commentListJson.hasNextPage;
        var nextPage=commentListJson.nextPage;
        var moreCommentData=commentListJson.data;
        if(!hasNextPage){
          return false;
        }
        magazineCommentResource.save({
          book_detail_id:bdId,
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.commentList.length<pageSize){
            hasNextPage = false;
          }
          moreCommentData = moreCommentData.concat(response.commentList);
          commentListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreCommentData
          };
          $rootScope.$broadcast('Magazine.MoreCommentListUpdated');
        });
      },
      //是否有下一页
      hasNextPage: function() {
        if (commentListJson === undefined) {
          return false;
        }
        return commentListJson.hasNextPage;
      },
      //获取章节详情的标题
      getBdTitleJson:function(){
        return bdTitle;
      },
      getMemberInfo:function(){
        return memberInfo;
      },
      getCommentCount:function(bdId,memberId){
        commentCountResource.save({
          book_detail_id:bdId,
          member_id:memberId
        },function(reponse){
          commentCount = reponse;
          $rootScope.$broadcast('Magazine.CommentCountUpdated');
        })
      },
      //发表评论
      publishComment:function(bdId,memberId,newComment){
        publishCommentResource.save({
          member_id:memberId,
          book_detail_id:bdId,
          bd_comment_content:newComment
        },function(response){
          magazineCommentJson = response;
          $rootScope.$broadcast('Magazine.publishCommentUpdated');
        });
      },
      getCommentJson:function(){
        return commentListJson.data;
      },
      //获取到新增的评论内容
      getNewComment:function(){
        return magazineCommentJson;
      },
      //获取评论的数量
      getCommetCountJson:function(){
        return commentCount;
      }
    }
  })
;
